#!/bin/bash

# Slackware build script for re-typeset

# Written by B. Watson (yalhcru@gmail.com)

# Licensed under the WTFPL. See http://www.wtfpl.net/txt/copying/ for details.

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=re-typeset
VERSION=${VERSION:-1.2.0}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

# No need for SLKCFLAGS or LIBDIRSUFFIX.

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar xvf $CWD/$PRGNAM-$VERSION.tar.gz
cd $PRGNAM-$VERSION
chown -R root:root .
find -L .  -perm /111 -a \! -perm 755 -a -exec chmod 755 {} \+ -o \
        \! -perm /111 -a \! -perm 644 -a -exec chmod 644 {} \+

# The Language menu doesn't actually work. For now, it's disabled,
# unless/until someone fixes it (upstream? me? *you*?). If you build
# with LANGMENU=yes, you'll see the menu with 2 entries (English
# and Polish), but it won't actually *do* anything. With LANGMENU=no,
# the UI is English only.
LANGMENU="${LANGMENU:-no}"

if [ "$LANGMENU" = "yes" ]; then
  # Look in /usr/share/$PRGNAM for language icons and translations.
  patch -p1 < $CWD/paths.diff
else
  # Remove the Languages menu entirely.
  patch -p1 < $CWD/rm_lang_menu.diff
fi

# The Makefile is a mess... and it's intended for the author's particular
# setup with various cross-compilers installed in his home directory &
# such. Fortunately we can just do this for a native build (and qmake-qt5
# will set CXXFLAGS to the SLKCFLAGS it was compiled with, too):
cd $PRGNAM-src
qmake-qt5
make

# No install target, just put everything where it goes.
mkdir -p $PKG/usr/bin $PKG/usr/share/pixmaps
install -s -m0755 $PRGNAM $PKG/usr/bin
cat img/$PRGNAM.png > $PKG/usr/share/pixmaps/$PRGNAM.png

if [ "$LANGMENU" = "yes" ]; then
  # Translations don't get built automatically.
  mkdir -p $PKG/usr/share/$PRGNAM
  lrelease-qt5 lang/*.ts
  for i in img/??.png lang/*.qm; do
    cat $i > $PKG/usr/share/$PRGNAM/$( basename $i )
  done
fi

# .desktop file written by SlackBuild author. Sorry about the lame little
# 16x16 icon, it's all upstream gave us.
mkdir -p $PKG/usr/share/applications
cat $CWD/$PRGNAM.desktop > $PKG/usr/share/applications/$PRGNAM.desktop

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a LICENSE.TXT ../README.TXT $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
